package com.comeval.persistencia;

import com.comeval.modelo.Entrada;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DianaMarcela
 */
@Stateless
public class EntradaDAO implements IEntradaDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrarEntrada(Entrada entrada) {
        try {
            entityManager.persist(entrada);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void modificarEntrada(Entrada entrada) {
        try {
            entityManager.merge(entrada);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void eliminarEntrada(Entrada entrada) {
        try {
            entityManager.remove(entrada);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Entrada> consultarEntradas() {
        try {
            String jpql = "Select entrada From Entrada entrada";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Entrada consultarPorCodigo(Long codigoEntrada) {
        try {
            return entityManager.find(Entrada.class, codigoEntrada);
        } catch (RuntimeException e) {
            throw e;
        }
    }

}
